import myAxios from '../myAxios/myAxios.js';
import { GetSuJiRequire } from './GetSuJiRequire.js';
import { GetWaiXiaoRequire } from './GetWaiXiaoRequire.js';
import { GetJXSRequire } from './GetJXSRequire.js';
import { GetBuyTypeTable } from './getBuyTypeTable.js';

let metCodePropString = '';
let metCodeParamString = '';
// 最终查询物料号
let metCodeParamList = [];
// 数据库中存的物料号
let metCodeInDatabaseParamList = [];

let GetMatCodeParamString = async function () {
    try {
        // 测试
        test();

        //#region 正式逻辑 
        const metCodeTable = await myAxios.get('陈帅廷查询周计划_驱动物料号', '', 'Q902');

        for (const row of metCodeTable) {
            let matcode = row.METCODE.toString();
            matcode = matcode.replace('？', '').trim();
            metCodeParamString += (metCodeParamString === '' ? '' : ',') + matcode;
            metCodeParamList.push(matcode);
            metCodeInDatabaseParamList.push(matcode);
        }

        const sujiTable = await GetSuJiRequire();
        for (const row of sujiTable) {
            const matcode = row.MATNR.toString();
            if (!metCodeParamList.includes(matcode)) {
                metCodeParamList.push(matcode);
                metCodeParamString += (metCodeParamString === '' ? '' : ',') + matcode;
            }
        }

        const waixiaoTable = await GetWaiXiaoRequire();
        for (const row of waixiaoTable) {
            const matcode = row.MATNR.toString();
            if (!metCodeParamList.includes(matcode)) {
                metCodeParamList.push(matcode);
                metCodeParamString += (metCodeParamString === '' ? '' : ',') + matcode;
            }
        }

        const jxsTable = await GetJXSRequire();
        for (const row of jxsTable) {
            const matcode = row.MATCODE.toString();
            if (!metCodeParamList.includes(matcode)) {
                metCodeParamList.push(matcode);
                metCodeParamString += (metCodeParamString === '' ? '' : ',') + matcode;
            }
        }

        // 用物料号查采购类型,工厂Q902 AND 类型是E或者X的留下,DRIVE_METCODE表里物料号强制显示
        const buyTypeTable = await GetBuyTypeTable(metCodeInDatabaseParamList, metCodeParamString);
        // 更新查询参数,只查留下的物料号
        metCodePropString = '';
        metCodeParamString = '';

        for (let i = 0; i < buyTypeTable.length; i++) {
            const row = buyTypeTable[i];
            metCodePropString += (i === 0 ? '' : ',') + "'" + row.MATNR + "'";
            metCodeParamString += (metCodeParamString === '' ? '' : ',') + row.MATNR;
        }

        metCodePropString = '(' + metCodePropString + ')';

        return {
            metCodePropString,
            metCodeParamString,
            metCodeParamList,
            metCodeInDatabaseParamList,
        };
        //#endregion
    } catch (error) {
        console.error('Error processing data:', error);
        throw error;
    }
};

let test = async function () {

    // let a = await GetSuJiRequire();
    // let b = await GetWaiXiaoRequire();
    // let c = await GetJXSRequire();
};

export {
    GetMatCodeParamString,
};
